package org.datascooter.db.postgresql;

import java.sql.SQLException;

import javax.sql.ConnectionPoolDataSource;

import org.datascooter.impl.ContextConnector;
import org.datascooter.inface.IDataSource;
import org.postgresql.jdbc3.Jdbc3ConnectionPool;

public class PostgresqlConnector extends ContextConnector {

	private Jdbc3ConnectionPool source;

	public PostgresqlConnector() {
	}

	public PostgresqlConnector(IDataSource source) throws SQLException {
		super(source);
	}

	@Override
	public boolean isSupportPagination() {
		return true;
	}

	@Override
	public ConnectionPoolDataSource createPooledDataSource() throws SQLException {
		source = new Jdbc3ConnectionPool();
		String addrPortBase = dataSource.getURL().substring(dataSource.getURL().indexOf("//") + 2);
		String addr = addrPortBase.substring(0, addrPortBase.indexOf(":"));
		String port = addrPortBase.substring(addrPortBase.indexOf(":") + 1, addrPortBase.indexOf("/"));
		String dbName = addrPortBase.substring(addrPortBase.indexOf("/") + 1, addrPortBase.length());
		source.setServerName(addr);
		source.setPortNumber(Integer.valueOf(port));
		source.setDatabaseName(dbName);
		source.setUser(dataSource.getUser());
		source.setPassword(dataSource.getPassword());
		return source;
	}

	@Override
	public String getContextId() {
		return "postgresql";
	}

	@Override
	public boolean isCallParameterMetadata() {
		return true;
	}
}
